// 新增用户
let	mongoose = require('mongoose'),
    async = require('async'),
	memberUsers = mongoose.model('member_users');

module.exports = (req, res, next) => {
    let params = req.body,
        saveData = new memberUsers(params);
    
    async.series([
        callback => {
            memberUsers.find({}, (err, data) => {
                if(err){
                    res.json({
                        result: 0,
                        message: err
                    })
                }else{
                    let repeat = false,
                        errMsg = '';
                    data.forEach(e => {
                        switch(true){
                            case e.userName == params.userName:
                                errMsg = '用户名重复';
                                repeat = true;
                                break;
                            case e.mobile == params.mobile:
                                errMsg = '手机号码重复';
                                repeat = true;
                                break;
                            case e.email == params.email:
                                errMsg = '邮箱重复';
                                repeat = true;
                                break;
                            default: break;
                        }
                    });
                    
                    if(repeat){
                        res.json(resFail(errMsg))
                    }else{
                        callback();
                    }
                }
            })
        },
        callback => {
            saveData.save((err, data) => {
                if(err){
                    res.json(resFail('添加用户出错。'))
                }else{
                    res.json({
                        result: 1
                    })
                }
            });
        }
    ]);
};